﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using DataTransfer;

namespace BUS
{
  public  class BenhNhanBus
    {
        public string connect = QuiDinhBus.connect;
        public static QLPMDataContext db;
        // db = new QLPMDataContext(connect);
        public IEnumerable<object> GetAll()
        {
            IEnumerable<object> result = null;
            try
            {
                db = new QLPMDataContext(connect);
                result = from bn in db.BenhNhans
                         join pk in db.PhieuKhams on bn.IDPhieuKham equals pk.ID
                         select new { bn.ID, IDPK = pk.ID, bn.HoTen,bn.NgayKham, pk.LoaiBenh, pk.TrieuChung, bn.UserCreate, bn.CreateDate, bn.UserModify, bn.ModifyDate };
                //db.Dispose();
               
               

            }
            catch (Exception)
            {

                throw;
            }
            return result;


        }
        public IEnumerable<object> GetByDay(DateTime key)
        {
            IEnumerable<object> result = null;
            try
            {
                db = new QLPMDataContext(connect);
                result = from bn in db.BenhNhans
                         join pk in db.PhieuKhams on bn.IDPhieuKham equals pk.ID
                         where bn.NgayKham==key
                         select new { bn.ID, IDPK = pk.ID,bn.HoTen, bn.NgayKham, pk.LoaiBenh, pk.TrieuChung, bn.UserCreate, bn.CreateDate, bn.UserModify, bn.ModifyDate };
                //db.Dispose();



            }
            catch (Exception)
            {

                throw;
            }
            return result;


        }
        public int Insert(BenhNhan info)
        {
            int id = -1;
            try
            {
                db = new QLPMDataContext(connect);
                db.BenhNhans.InsertOnSubmit(info);
                db.SubmitChanges();

                id = info.ID;
            }
            catch (Exception)
            {

                throw;
            }

            return id;
        }
        public bool Delele(int key)
        {
            bool flag = false;
            try
            {

                db = new QLPMDataContext(connect);

               BenhNhan del = (from t in db.BenhNhans where t.ID == key select t).SingleOrDefault();

                db.BenhNhans.DeleteOnSubmit(del);

                db.SubmitChanges();

                flag = true;
            }
            catch (Exception)
            {

                throw;
            }
            return flag;
        }
        public BenhNhan Get(int key)
        {
            BenhNhan info = new BenhNhan();
            info = null;
            try
            {
                db = new QLPMDataContext(connect);
                info = (from qd in db.BenhNhans where qd.ID == key select qd).SingleOrDefault();
            }
            catch (Exception)
            {

                throw;
            }
            return info;
        }
        public bool Update(BenhNhan info)
        {
            bool flag = false;
            try
            {
                db = new QLPMDataContext(connect);
                var t = (from qd in db.BenhNhans where qd.ID == info.ID select qd).SingleOrDefault();
                t.IDPhieuKham = info.IDPhieuKham;
                t.HoTen = info.HoTen;
                t.GioiTinh = info.GioiTinh;
                t.NamSinh = info.NamSinh;
                t.DiaChi = info.DiaChi;
                t.NgayKham = info.NgayKham;
                t.UserModify = info.UserModify;
                t.ModifyDate = info.ModifyDate;
                db.SubmitChanges();


                flag = true;

            }
            catch (Exception)
            {

                throw;
            }
            return flag;
        }
    }
}
